package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.hzjq.data.entity.BlastServiceBean;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.databinding.ActivitySettingServiceBinding;
import com.tuanbiao.models.SettingModel;
import com.tuanbiao.presenter.setting.SettingServicePresenter;
import com.tuanbiao.utils.ClickUtil;

import java.util.List;

import timber.log.Timber;

public class SettingServiceActivity extends BasePresenterActivity<SettingServicePresenter, ActivitySettingServiceBinding> implements SettingController.ChangeServiceView {
    private SettingServiceAdapter settingServiceAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, SettingServiceActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivitySettingServiceBinding getBindingView() {
        return ActivitySettingServiceBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        settingServiceAdapter = new SettingServiceAdapter();
        settingServiceAdapter.addChildClickViewIds(R.id.cb_select, R.id.cb_is_debug);
        settingServiceAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            BlastServiceBean bean = (BlastServiceBean) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.cb_select:
                    AppCompatCheckBox select = (AppCompatCheckBox) view;
                    bean.setSelect(select.isChecked());
                    Timber.i("setSelect:"+select.isChecked());
                    adapter.notifyItemChanged(position);
                    break;
                case R.id.cb_is_debug:
                    AppCompatCheckBox debug = (AppCompatCheckBox) view;
                    bean.setDebug(debug.isChecked());
                    Timber.i("setDebug:"+debug.isChecked());
                    adapter.notifyItemChanged(position);
                    break;
            }
        });
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvContent.setAdapter(settingServiceAdapter);
        binding.btnCommit.setOnClickListener(view -> {
            mPresenter.changeServices(settingServiceAdapter.getData());
        });
    }

    @Override
    protected SettingServicePresenter createPresenter() {
        return new SettingServicePresenter(new SettingModel(), this);
    }


    @Override
    public void showServices(List<BlastServiceBean> allUploadService) {
        settingServiceAdapter.setNewInstance(allUploadService);
    }
}